package com.leetcode.tree;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @Author zhang.xin
 * @Description: TODO(这里描述)
 * @Date 2019/12/27
 */
public class LeetCode101 {

    /**
     * 这是官方题解
     * @param root 根节点
     * @return 返回是否镜像
     */
    public boolean isSymmetric(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode left = queue.poll();
            TreeNode right = queue.poll();
            if (left == null && right == null)
                continue;
            if (left ==null || right == null)
                return false;
            if (left.val != right.val)
                return false;
            queue.add(left.left);
            queue.add(right.right);
            queue.add(left.right);
            queue.add(right.left);
        }
        return true;
    }

    private TreeNode creatTree() {
        TreeNode t1 = new TreeNode(1);
        TreeNode t2 = new TreeNode(2);
        TreeNode t3 = new TreeNode(2);
        TreeNode t4 = new TreeNode(3);
        TreeNode t6 = new TreeNode(4);
        TreeNode t5 = new TreeNode(3);
        TreeNode t7 = new TreeNode(4);
        t1.left =t2;
        t1.right = t3;
        t2.left = t6;
        t2.right = t4;
        t3.left = t5;
        t3.right =t7;
        return t1;
    }

    public static void main(String[] args) {
        LeetCode101 test = new LeetCode101();
        TreeNode root = test.creatTree();
        System.out.println(test.isSymmetric(root));
    }

}
